<?php
require_once ('./class/H.class.php');
require_once ('./class/database.class.php');
require_once ("./class/Snoopy.class.php");
savePics(1,500);
function savePics($p = 1,$pageSz = 50){
    global $dbm;
    $listData = $dbm->select('tb_imgs',['id','url'],["ORDER"=>['id'=>"ASC"],'LIMIT' => [($p-1)*$pageSz,$pageSz]]);
    if(count($listData) == 0){
        echo "already completes...".chr(10);
        return;
    }

    foreach($listData as $lk => $lv){
        $dir = "uploads/images/".md5($lv['url'] .time()).'.jpg';
        $data = @file_get_contents($lv['url']);
        if(!empty($data)){
            $b = @file_put_contents($dir,$data);
            if($b){
                $ret = $dbm->update("tb_imgs",[
                    'cdn' => $dir
                ],[
                    'id' => $lv['id']
                ]);
                if($ret){
                    echo date('H:i:s',time()).'-->'.$lv['id'].'-->OK'.chr(10);
                } else {
                    @file_put_contents('debug',date('H:i:s',time()).'-->'.$lv['id'].'-->'.$lv['url'].'-->error',FILE_APPEND);
                }
            }
        }
    }
    savePics($p+1,$pageSz);
}